package com.yh.modules.data.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.plugins.Page;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.yh.common.utils.Assert;
import com.yh.common.utils.PageUtils;
import com.yh.common.utils.R;
import com.yh.common.utils.ValidatorUtils;
import com.yh.modules.data.entity.*;
import com.yh.modules.data.form.MachineQueryForm;
import com.yh.modules.data.service.MachineDailyInfoService;
import com.yh.modules.data.service.MachineErrorInfoService;
import com.yh.modules.data.service.MachineWorkRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @Author: skh
 * @Date: 2020/3/23 15:00
 * @Description: 机器日常信息和错误信息, 主要是为了完成印尼的需求
 */
@RestController
@RequestMapping("/machine/info")
public class MachineInfoController {

    @Autowired
    private MachineErrorInfoService machineErrorInfoService;

    @Autowired
    private MachineDailyInfoService machineDailyInfoService;

    @Autowired
    private MachineWorkRecordService machineWorkRecordService;

    //新增错误信息
    @PostMapping("/error/save")
    public R saveErrorInfo(@RequestBody MachineErrorInfoEntity reason) {
        ValidatorUtils.validateEntity(reason);

        machineErrorInfoService.saveMachineErrorReason(reason);

        return R.ok();
    }

    //查询错误信息
    @GetMapping("/error/list")
    public R getErrorInfoList(@RequestParam Map<String, Object> params) {

        PageUtils pageUtils = machineErrorInfoService.getErrorInfoList(params);

        return R.ok().put("page", pageUtils);
    }

    //获取错误信息列表的ColNamesAndModel
    @RequestMapping("/error/getColNamesAndModel/{machineId}")
    public R getErrorColNamesAndModel(@PathVariable("machineId") Integer machineId) {

        JqGridEntity jqGridEntity = machineErrorInfoService.getErrorColNamesAndModel(machineId);

        return R.ok().put("jqGridEntity", jqGridEntity);
    }

    //新增日常工作信息
    @PostMapping("/daily/save")
    public R saveDailyInfo(@RequestBody MachineDailyInfoEntity dailyInfoEntity) {
        ValidatorUtils.validateEntity(dailyInfoEntity);

        machineDailyInfoService.saveMachineDailyInfo(dailyInfoEntity);

        return R.ok();
    }

    //查询日常工作信息类比
    @GetMapping("/daily/list")
    public R getDailyInfoList(@RequestParam Map<String, Object> params) {

        PageUtils pageUtils = machineDailyInfoService.getDailyInfoList(params);

        return R.ok().put("page", pageUtils);
    }

    //获取日常工作信息列表的ColNamesAndModel
    @RequestMapping("/daily/getColNamesAndModel/{machineId}")
    public R getDailyColNamesAndModel(@PathVariable("machineId") Integer machineId) {

        JqGridEntity jqGridEntity = machineDailyInfoService.getDailyColNamesAndModel(machineId);

        return R.ok().put("jqGridEntity", jqGridEntity);
    }

    //获取机器零件生产总数统计
    @GetMapping("/count/part/num")
    public R countMachinePartSum(MachineQueryForm machineQueryForm) {
        Assert.isNull(machineQueryForm.getMachineId(), "机器id不能为空");
        List<MachinePartCount> machinePartCounts = machineDailyInfoService.countMachinePartSum(
                machineQueryForm.getMachineId(), machineQueryForm.getStartTime(), machineQueryForm.getEndTime());
        return R.ok().put("page", machinePartCounts);
    }

    //获取日常工作信息列表的ColNamesAndModel
    @RequestMapping("/part/count/getColNamesAndModel/{machineId}")
    public R getPartCountColNamesAndModel(@PathVariable("machineId") Integer machineId) {

        JqGridEntity jqGridEntity = machineDailyInfoService.getPartCountColNamesAndModel(machineId);

        return R.ok().put("jqGridEntity", jqGridEntity);
    }

    //新增机器工作记录
    @PostMapping("/workRecord/save")
    public R saveWorkRecord(@RequestBody MachineWorkRecordEntity workRecordEntity) {
        ValidatorUtils.validateEntity(workRecordEntity);
        machineWorkRecordService.saveWorkRecord(workRecordEntity);
        return R.ok();
    }

    //获取机器工作记录
    @GetMapping("/workRecord/list")
    public R getWorkRecordList(@RequestParam Map<String, Object> params) {
        Page page = machineWorkRecordService.getWorkRecordList(params);

        List<MachineWorkRecordEntity> records = page.getRecords();

        //获取图表信息
        //xData
        List<String> xList = records.stream().map(new Function<MachineWorkRecordEntity, String>() {
            @Override
            public String apply(MachineWorkRecordEntity workRecordEntity) {
                Date startTime = workRecordEntity.getStartTime();
                String sTime = DateUtil.formatTime(startTime);

                Date endTime = workRecordEntity.getEndTime();
                String eTime = DateUtil.formatTime(endTime);

                return sTime + "-" + eTime;
            }
        }).collect(Collectors.toList());

        //valueData
        List<Integer> valueList = records.stream().map(new Function<MachineWorkRecordEntity, Integer>() {

            @Override
            public Integer apply(MachineWorkRecordEntity workRecordEntity) {
                return workRecordEntity.getTotalCount();
            }
        }).collect(Collectors.toList());

        PageUtils pageUtils = new PageUtils(page);

        return R.ok().put("page", pageUtils).put("xList",xList).put("valueList",valueList);
    }



    //获取机器工作记录列表的ColNamesAndModel
    @RequestMapping("/workRecord/getColNamesAndModel/{machineId}")
    public R getWorkRecordColNamesAndModel(@PathVariable("machineId") Integer machineId) {

        JqGridEntity jqGridEntity = machineWorkRecordService.getWorkRecordColNamesAndModel(machineId);

        return R.ok().put("jqGridEntity", jqGridEntity);
    }
}
